﻿/*
 * Imigrasi SMS (Special built for Immigration Office)
 * Copyright (C) 2010 Wish Enterprise Development Team

 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * Contact Us:
 * E-Mail   : contact@wishenterprise.com     Website: http://www.wishenterprise.com
 * 
 * */

using System;
using System.Linq;
using System.Windows.Forms;
using Origami.Data.Provider;
using SmsEntity;

namespace SmsRepository
{
    /// <summary>
    /// DbLogOn User Interface
    /// </summary>
    public partial class DBLogonUI : Form
    {
        #region Constructors / Destructors

        /// <summary>
        /// Constructor DbLogOnUI
        /// </summary>
        public DBLogonUI()
        {
            InitializeComponent();
        }

        #endregion

// ReSharper disable InconsistentNaming
        private void btnSave_Click(object sender, EventArgs e)
// ReSharper restore InconsistentNaming
        {
            DBLogonInfo obj;
            try
            {
                obj = new DBLogonInfo(
                    txtApplicationId.Text.Trim(),
                    txtProvider.Text.Trim(),
                    CreateConnectionString());
            }
            catch (ArgumentNullException ex)
            {
                MessageBox.Show(ex.Message,
                                @"Warning!",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
                return;
            }

            using (var xml =
                new XmlConfiguration(obj))
                if (xml.Write())
                {
                    ClearForm();
                }
        }

        /// <summary>
        /// Clears the form.
        /// </summary>
        private void ClearForm()
        {
            txtApplicationId.Enabled = true;
            txtApplicationId.Clear();
            txtApplicationId.Focus();
            txtServer.Clear();
            txtDatabase.Clear();
            txtUserId.Clear();
            txtPassword.Clear();
            txtPort.Clear();
        }

        /// <summary>
        /// Creates the connection string.
        /// </summary>
        /// <returns></returns>
        private string CreateConnectionString()
        {
            return string.Format("server={0};port={1};user id={2};password={3};database={4}",
                                 txtServer.Text.Trim(),
                                 txtPort.Text.Trim(),
                                 txtUserId.Text.Trim(),
                                 txtPassword.Text.Trim(),
                                 txtDatabase.Text.Trim());
        }

// ReSharper disable InconsistentNaming
        private void btnTest_Click(object sender, EventArgs e)
// ReSharper restore InconsistentNaming
        {
            try
            {
                ConnectionFactory.Build(
                    new DataSource
                        {
                            Provider = txtProvider.Text.Trim(),
                            ConnectionString = CreateConnectionString()
                        });
                MessageBox.Show(@"Test Connection Successfully!",
                                @"Test",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("{0}\r\n\r\n{1}", "Test Connection Failed!", ex.Message),
                                @"Test",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }
        }

// ReSharper disable InconsistentNaming
        private void txtApplicationId_Validated(object sender, EventArgs e)
// ReSharper restore InconsistentNaming
        {
            if (string.IsNullOrEmpty(txtApplicationId.Text.Trim())) return;

            using (var xml =
                new XmlConfiguration(new DBLogonInfo(txtApplicationId.Text.Trim())))
            {
                if (xml.Read())
                {
                    txtProvider.Text = xml.DBLogonInfo.Provider;
                    txtApplicationId.Enabled = false;
                    string[] strConn = xml.DBLogonInfo.ConnectionString.Split(';');

                    foreach (var strItem in strConn.Select(s => s.Split('=')))
                    {
                        switch (strItem[0].Trim())
                        {
                            case "server":
                                txtServer.Text = strItem[1].Trim();
                                break;
                            case "port":
                                txtPort.Text = strItem[1].Trim();
                                break;
                            case "user id":
                                txtUserId.Text = strItem[1].Trim();
                                break;
                            case "password":
                                txtPassword.Text = strItem[1].Trim();
                                break;
                            case "database":
                                txtDatabase.Text = strItem[1].Trim();
                                break;
                        }
                    }
                }
            }
        }

// ReSharper disable InconsistentNaming
        private void btnClear_Click(object sender, EventArgs e)
// ReSharper restore InconsistentNaming
        {
            ClearForm();
        }
    }
}